package com.hegongda.okcmall.product.service.impl;

import com.hegongda.okcmall.product.vo.Images;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hegongda.common.utils.PageUtils;
import com.hegongda.common.utils.Query;

import com.hegongda.okcmall.product.dao.SkuImagesDao;
import com.hegongda.okcmall.product.entity.SkuImagesEntity;
import com.hegongda.okcmall.product.service.SkuImagesService;


@Service("skuImagesService")
public class SkuImagesServiceImpl extends ServiceImpl<SkuImagesDao, SkuImagesEntity> implements SkuImagesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuImagesEntity> page = this.page(
                new Query<SkuImagesEntity>().getPage(params),
                new QueryWrapper<SkuImagesEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuImage(Long skuId, List<Images> images) {
        if (images != null && images.size() > 0){
            List<SkuImagesEntity> collect = images.stream().filter(image -> StringUtils.isNotEmpty(image.getImgUrl())).map(image -> {
                SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                BeanUtils.copyProperties(image, skuImagesEntity);
                skuImagesEntity.setSkuId(skuId);
                skuImagesEntity.setImgSort(1);
                return skuImagesEntity;
            }).collect(Collectors.toList());
            this.saveBatch(collect);
        }
    }

}